<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
	<!-- 页面meta -->
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>类型模板管理</title>
	<!-- Tell the browser to be responsive to screen width -->
	<meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
	<link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
	<link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
	<link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
	<link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
	<script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="../plugins/select2/select2.css" />
	<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
	<script src="/plugins/select2/select2.min.js" type="text/javascript"></script>
	<script type="text/javascript" src = "../plugins/bootstrap-table/dist/bootstrap-table.js"></script>
	<script type="text/javascript" src = "../plugins/bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script>
	<link rel="stylesheet" type="text/css" href="../plugins/bootstrap-table/dist/bootstrap-table.css">
	<script>
		function searchByName() {
			$("#dataList").bootstrapTable("refresh")
		}
	</script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
<!-- .box-body -->

<div class="box-header with-border">
	<h3 class="box-title">商品类型模板管理</h3>
</div>

<div class="box-body">

	<!-- 数据表格 -->
	<div class="table-box">

		<!--工具栏-->
		<div class="pull-left">
			<div class="form-group form-inline">
				<div class="btn-group">
					<button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
					<button type="button" class="btn btn-default" title="删除" onclick = "deleteAll()"><i class="fa fa-trash-o"></i> 删除</button>

					<button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
				</div>
			</div>
		</div>
		<div class="box-tools pull-right">
			<div class="has-feedback">
				分类模板名称：<input type = "text" id = "searchTemplate_id">
				<button class="btn btn-default" onclick="searchByName()">查询</button>
			</div>
		</div>
		<!--工具栏/-->

		<!--数据列表-->
		<table id="dataList" class="table table-bordered table-striped table-hover dataTable">

		</table>
		<!--数据列表/-->


	</div>
	<!-- 数据表格 /-->




</div>
<!-- /.box-body -->




<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog" >
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h3 id="myModalLabel">商品类型模板编辑</h3>
			</div>
			<div class="modal-body">

				<table class="table table-bordered table-striped"  width="800px" id = "addTemp_id">
					<tr>
						<td>商品类型</td>
						<td><input  class="form-control" placeholder="商品类型" id = "name">  </td>
					</tr>
					<tr>
						<td>关联品牌</td>
						<td>
							<select id = "brand_id" name = "brandIds" multiple style="width:100%">
								<option value = "-1">请选择</option>
								<option th:each = "b : ${brandList}" th:value="${b.id}">[[${b.name}]]</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>关联规格</td>
						<td>
							<select id = "spec_id" name = "specIds" multiple style="width:100%">
								<option value = "-1">请选择</option>
								<option th:each = "s : ${specList}" th:value="${s.id}">[[${s.specName}]]</option>
							</select>
						</td>
					</tr>

					<tr>
						<td>扩展属性</td>
						<td>
							<div class="btn-group">
								<button type="button" class="btn btn-default" title="新增扩展属性" onclick="addRow()"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							</div>
							<table class="table table-bordered table-striped"  width="800px" id = "kuozhan_id">
								<thead>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>
									<td>操作</td>
								</tr>
								</thead>
								<tbody></tbody>
							</table>

						</td>
					</tr>

				</table>

			</div>
			<div class="modal-footer">
				<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick = "addTemplate()">保存</button>
				<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			</div>
		</div>
	</div>
</div>

<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog" >
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h3 id="myModalLabel">商品类型模板修改</h3>
			</div>
			<div class="modal-body">

				<table class="table table-bordered table-striped"  width="800px" id = "updateTemp_id">
					<tr>
						<td>商品类型</td>
						<td>
							<input type = "hidden" id = "updateId">
							<input  class="form-control" placeholder="商品类型" id = "updateName">
						</td>
					</tr>
					<tr>
						<td>关联品牌</td>
						<td>
							<select id = "updateBrand_id" name = "brandIds" multiple style="width:100%">
								<option value = "-1">请选择</option>
								<option th:each = "b : ${brandList}" th:value="${b.id}">[[${b.name}]]</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>关联规格</td>
						<td>
							<select id = "updateSpec_id" name = "specIds" multiple style="width:100%">
								<option value = "-1">请选择</option>
								<option th:each = "s : ${specList}" th:value="${s.id}">[[${s.specName}]]</option>
							</select>
						</td>
					</tr>

					<tr>
						<td>扩展属性</td>
						<td>
							<div class="btn-group">
								<button type="button" class="btn btn-default" title="新增扩展属性" onclick="addRows()"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							</div>
							<table class="table table-bordered table-striped"  width="800px" id = "updateKuozhan_id">
								<thead>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>
									<td>操作</td>
								</tr>
								</thead>
								<tbody id = "updateKZ_id"></tbody>
							</table>

						</td>
					</tr>

				</table>

			</div>
			<div class="modal-footer">
				<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick = "updateTemplate()">保存</button>
				<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			</div>
		</div>
	</div>
</div>

<script>
	//批量删除
	function deleteAll() {
		var arr = $("#dataList").bootstrapTable("getSelections")
		var ids = []
		for (var i = 0; i < arr.length; i++) {
			ids.push(arr[i].id)
		}
		$.ajax({
			url : "/tempalte/deleteAll",
			type : "post",
			dataType : "json",
			data : {ids : ids},
			traditional : true,
			success : function (rs) {
				alert(rs.message)
			}
		})
	}
	//修改
	function updateTemplate() {
		var options = $("#updateBrand_id").select2("data")
		var brandId = []
		for (var i = 0; i < options.length; i++) {
			var str = {id : options[i].id, text : options[i].text}
			brandId.push(str)
		}
		var brandIds = JSON.stringify(brandId)
		var option = $("#updateSpec_id").select2("data")
		var specId = []
		for (var i = 0; i < option.length; i++) {
			var str = {id :option[i].id, text : option[i].text}
			specId.push(str)
		}
		var specIds = JSON.stringify(specId)
		var trs = $("#updateKuozhan_id").find("tr")
		var customAttributeItem = []
		for (var i = 1; i < trs.length; i++) {
			var tds = $(trs[i]).find("td")
			var input = $(tds[1]).find("input")
			var customAttrValue = $(input).val()
			var str = {text : customAttrValue}
			customAttributeItem.push(str)
		}
		var customAttributeItems = JSON.stringify(customAttributeItem)
		$.ajax({
			url : "/tempalte/updateTemplate",
			type : "get",
			dataType : "json",
			data : {name : $("#updateName").val(), id : $("#updateId").val(), specIds : specIds, brandIds : brandIds, customAttributeItems : customAttributeItems},
			success : function (rs) {
				alert(rs.message)
				$("#dataList").bootstrapTable("refresh")
			}
		})
	}
	//增加
	function addTemplate() {
		var options = $("#brand_id").select2("data")
		var brandId = []
		for (var i = 0; i < options.length; i++) {
			var str = {id : options[i].id, text : options[i].text}
			brandId.push(str)
		}
		var brandIds = JSON.stringify(brandId)
		var option = $("#spec_id").select2("data")
		var specId = []
		for (var i = 0; i < option.length; i++) {
			var str = {id :option[i].id, text : option[i].text}
			specId.push(str)
		}
		var specIds = JSON.stringify(specId)
		var trs = $("#kuozhan_id").find("tr")
		var customAttributeItem = []
		for (var i = 1; i < trs.length; i++) {
			var tds = $(trs[i]).find("td")
			var input = $(tds[1]).find("input")
			var customAttrValue = $(input).val()
			var str = {text : customAttrValue}
			customAttributeItem.push(str)
		}
		var customAttributeItems = JSON.stringify(customAttributeItem)
		$.ajax({
			url : "/tempalte/addTemplate",
			type : "post",
			dataType : "json",
			data : {name : $("#name").val(), specIds : specIds, brandIds : brandIds, customAttributeItems : customAttributeItems},
			success : function (rs) {
				alert(rs.message)
				$("#dataList").bootstrapTable("refresh")
			}
		})
	}

	//增加行
	function addRow() {
		var ta = document.getElementById("kuozhan_id")
		var tr = document.createElement("tr")
		tr.innerHTML = '<td><input type="checkbox" class="icheckbox_square-blue" ></td>' +
				'<td><input class="form-control" placeholder="属性名称" name = "customAttributeItems" ></td>' +
				'<td><button type="button" class="btn btn-default" title="删除" onclick = "deleteRow(this)"><i class="fa fa-trash-o"></i> 删除</button></td>'
		ta.appendChild(tr)
	}
	//删除行
	function deleteRow(obj) {
		var ta = document.getElementById("kuozhan_id")
		var td = obj.parentNode
		var tr = td.parentNode
		ta.removeChild(tr)
	}
	//增加行
	function addRows() {
		var ta = document.getElementById("updateKZ_id")
		var tr = document.createElement("tr")
		tr.innerHTML = '<td><input type="checkbox" class="icheckbox_square-blue" ></td>' +
				'<td><input class="form-control" placeholder="属性名称" name = "customAttributeItems" ></td>' +
				'<td><button type="button" class="btn btn-default" title="删除" onclick = "deleteRows(this)"><i class="fa fa-trash-o"></i> 删除</button></td>'
		ta.appendChild(tr)
	}
	//删除行
	function deleteRows(obj) {
		var ta = document.getElementById("updateKZ_id")
		var td = obj.parentNode
		var tr = td.parentNode
		ta.removeChild(tr)
	}

	//页面加载
	$(function () {
		loadTemplatePage()
		$("#brand_id").select2()
		$("#spec_id").select2()
		$("#updateBrand_id").select2()
		$("#updateSpec_id").select2()
	})

	//查询
	function loadTemplatePage() {
		$("#dataList").bootstrapTable({
			url : "/tempalte/getTemplateInfo",
			method : "GET",
			cache : false,
			pagination : true,
			sidePagination : "server",
			pageNumber : 1,
			pageSize : 5,
			pageList : [5, 10, 15],
			queryParams : function (params) {
				var temp = {
					pageSize : params.limit,
					pageNumber : (params.offset / params.limit) + 1,
					name : $("#searchTemplate_id").val(),
				}
				return temp
			},
			columns : [
				{
					checkbox : true,
					visible : true
				},
				{
					field : "id",
					title : "模板ID"
				},{
					field : "name",
					title : "模板名称"
				}, {
					field : "brandIds",
					title : "关联品牌",
					formatter : function (value, row, index) {
						var brandList = JSON.parse(value)
						var str = ""
						for (var i = 0; i < brandList.length; i++) {
							if (i == brandList.length - 1) {
								str += brandList[i].text
							}else {
								str += brandList[i].text + ","
							}
						}
						return str
					}
				}, {
					field : "specIds",
					title : "关联规格",
					formatter : function (value, row, index) {
						var specList = JSON.parse(value)
						var str = ""
						for (var i = 0; i < specList.length; i++) {
							if (i == specList.length - 1) {
								str += specList[i].text
							}else {
								str += specList[i].text + ","
							}
						}
						return str
					}
				},{
					field : "customAttributeItems",
					title : "扩展属性",
					formatter : function (value, row, index) {
						var cousList = JSON.parse(value)
						var str = ""
						for (var i = 0; i < cousList.length; i++) {
							if (i == cousList.length - 1) {
								str += cousList[i].text
							}else {
								str += cousList[i].text + ","
							}
						}
						return str
					}
				}, {
					field : "id",
					title : "操作",
					formatter : function (value, row, index) {
						var result = ""
						result += "<a href = 'javascript:;' class = 'btn btn-xs blue' data-toggle='modal' onclick='setTemplateValue(" + value + ")' data-target='#updateModal'><span class = 'glyphicon glyphicon-pencil'></span></a>"
						result += "<a href = 'javascript:;' class = 'btn btn-xs blue' onclick = 'deleteTemplate(" + value + ")'><span class = 'glyphicon glyphicon-remove'></span></a>"
						return result
					}
				}

			]
		})
	}

	//单条删除
	function deleteTemplate(obj) {
		$.get("/tempalte/deleteTemplate?id=" + obj, function (rs) {
			alert(rs.message)
			$("#dataList").bootstrapTable("refresh")
		})
	}

	//回显
	function setTemplateValue(obj) {
		$.ajax({
			url : "/tempalte/getOneTemplate",
			type : "post",
			dataType : "json",
			data : {id : obj},
			success : function (rs) {
				console.log(rs)
				$("#updateId").val(rs.data.id)
				$("#updateName").val(rs.data.name)
				var brandId = JSON.parse(rs.data.brandIds)
				var brandIds = []
				for (var i = 0; i < brandId.length; i++) {
					brandIds.push(brandId[i].id)
				}
				$("#updateBrand_id").val(brandIds).trigger("change")
				var specId = JSON.parse(rs.data.specIds)
				var specIds = []
				for (var i = 0; i < specId.length; i++) {
					specIds.push(specId[i].id)
				}
				$("#updateSpec_id").val(specIds).trigger("change")
				var cus = JSON.parse(rs.data.customAttributeItems)
				var tr = ""
				for (var i = 0; i < cus.length; i++) {
					tr += '<tr><td><input type="checkbox" class="icheckbox_square-blue" ></td>' +
							'<td><input class="form-control" placeholder="属性名称" name = "customAttributeItems" value = "' + cus[i].text + '" ></td>' +
							'<td><button type="button" class="btn btn-default" title="删除" onclick = "deleteRows(this)"><i class="fa fa-trash-o"></i> 删除</button></td></tr>'
				}
				$("#updateKZ_id").html(tr)
			}
		})
	}
</script>
</body>

</html>